package com.ctrip.frame;

import com.ctrip.business.UserService;
import com.go.commons.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

@Slf4j
public class LoginFrame extends JFrame {
    //定义组件
    JPanel panelUsername, panelPassword, panelButton;
    JLabel lblCookie, lbelbPassword;
    JButton btnLogin, btnCancel;
    JTextArea fieldCookie;
    JScrollPane jspCookie;
    JPasswordField fieldPassword;

    //构造方法
    public LoginFrame() {
        super("抢单-登录");
        initGUI();
    }

    public static void main(String[] args) {
        new LoginFrame().setVisible(true);
    }

    void initGUI() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        //创建组件
//        panelUsername = new JPanel();
//        panelUsername.setBackground(new Color(131, 169, 233));
//        panelPassword = new JPanel();
//        panelPassword.setBackground(new Color(131, 169, 233));
        panelButton = new JPanel();
        panelButton.setBackground(Theme.backgroundColor);

        lblCookie = new JLabel("Cookie：");
        lblCookie.setFont(new Font(Theme.fontFamily, Font.BOLD, Theme.fontSize));

//        lbelbPassword = new JLabel("密　码：");
//        lbelbPassword.setFont(new Font(Theme.fontFamily, Font.BOLD, Theme.fontSize));

        btnLogin = new JButton("登录");
//        btnCancel = new JButton("取消");

        fieldCookie = new JTextArea();
        fieldCookie.setLineWrap(true);
        fieldCookie.setFont(new Font(Theme.fontFamily, Font.BOLD, Theme.fontSize));
//        fieldHost.setSize(200,50);
//        fieldPassword = new JPasswordField(15);
////        fieldPassword.setSize(200,50);
//        fieldPassword.setFont(new Font(Theme.fontFamily, Font.BOLD, Theme.fontSize));


        jspCookie = new JScrollPane(fieldCookie);
        jspCookie.getVerticalScrollBar().setUI(new CBScrollBarUI());
        jspCookie.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jspCookie.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jspCookie.setSize(new Dimension(300, 100));

        Image image = this.getToolkit().getImage(getClass().getClassLoader().getResource("images/logo.png"));
        this.setIconImage(image);
        //设置布局管理器
//        setLayout(new GridLayout(3, 1));
        this.add(lblCookie, new GBC(0, 0, 1, 2)
                .setIpad(0, 0)
                .setWeight(0, 0)
                .setAnchor(GBC.EAST)
                .setFill(GBC.BOTH)
                .setInsets(5, 5, 5, 5));
        this.add(jspCookie, new GBC(1, 0, 3, 2)
                .setIpad(10, 10).setWeight(1, 1).setAnchor(GBC.WEST).setFill(GBC.BOTH).setInsets(5, 5, 5, 5));
//        this.add(lbelbPassword, new GBC(0, 1, 1, 1)
//                .setIpad(0, 50).setWeight(0, 0).setAnchor(GBC.EAST).setFill(GBC.BOTH).setInsets(5, 5, 5, 5));
//        this.add(fieldPassword, new GBC(1, 1, 2, 1)
//                .setIpad(50, 50).setWeight(0, 0).setAnchor(GBC.WEST).setFill(GBC.BOTH).setInsets(5, 5, 5, 5));
        this.add(btnLogin, new GBC(1, 4, 3, 1)
                .setIpad(30, 20).setWeight(0, 0).setAnchor(GBC.WEST).setFill(GBC.VERTICAL).setInsets(10, 5, 5, 20));
        //加入组件
//        panelUsername.add(lblHost);
//        panelUsername.add(fieldHost);
//        panelPassword.add(lbelPort);
//        panelPassword.add(fieldPassword);
//        panelButton.add(btnOk);
//        panelButton.add(btnCancel);
//        Container container = getContentPane();
//        //加入到JFrame
//        container.add(panelUsername);
//        container.add(panelPassword);
//        container.add(panelButton);
//        this.setUndecorated(true); // 去掉窗口的装饰
//        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);//采用指定的窗口装饰风格
        //设置窗体
        setSize(350, 200);
        getContentPane().setBackground(Theme.backgroundColor);
//        setOpacity(0.8f);
//        setLocation(500,500);
        setResizable(false);

        setLocationRelativeTo(null);
//        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        fieldCookie.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_ENTER:
                        login();
                        break;
                    case KeyEvent.VK_ESCAPE:
                        System.exit(0);
                }
            }
        });
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });

//        btnCancel.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                System.exit(0);
//            }
//        });

    }

    void login() {
        String cookie = fieldCookie.getText();
//        String password = String.valueOf(fieldPassword.getPassword());
        if (StringUtils.isEmpty(cookie)) {
            JOptionPane.showMessageDialog(LoginFrame.this, "Cookie不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                UserService.initData(cookie);
                if (UserService.userName != null) {
//                    Map<String, Object> user = UserService.check(UserService.userName);
//                    if (user != null) {
//                        //跳转页面
//                        log.info("{}=>登录成功", cookie);
//                        //调到主窗口
//                        LoginFrame.this.setVisible(false);
//                        LoginFrame.this.dispose();
//                        new OrderFrame().setVisible(true);
//                    }else{
//                        JOptionPane.showMessageDialog(LoginFrame.this, "系统异常", "登录失败", JOptionPane.ERROR_MESSAGE);
//                    }
                    LoginFrame.this.setVisible(false);
                    LoginFrame.this.dispose();
                    new OrderFrame().setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(LoginFrame.this, "Cookie格式错误", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e1) {
                log.error("{}=>登录失败:{}", cookie, e1.getMessage());
                log.error(e1.toString(), e1);
                JOptionPane.showMessageDialog(LoginFrame.this, e1.getMessage(), "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

}
